package com.gemptc.daoimp;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import com.gemptc.bean.UserBean;
import com.gemptc.dao.Logindao;
import com.gemptc.utils.DBUtil;
import com.gemptc.utils.MD5Tool;


public class LoginImp implements Logindao {

	@Override
	public String logincheck(String user_name, String user_password) {
		String flag = "0";// 表示登录失败
		// 验证过程，查询数据库中的用户输入名对应的密码，是否和用户自己的输入密码匹配
		String sql = "select user_password,user_id,user_nickname from users where user_name=? ";
		Connection connection = DBUtil.getConnection();
		PreparedStatement preparedStatement = null;
		ResultSet resultSet = null;

		try {
			preparedStatement = connection.prepareStatement(sql);
			// 设置对应值
			preparedStatement.setString(1, user_name);
			// 查询
			resultSet = preparedStatement.executeQuery();
			while (resultSet.next()) {

				String password2 = resultSet.getString(1);
				
				if (password2.equals(MD5Tool.MD5(user_password))) {
					flag =String.valueOf(resultSet.getInt(2));// 表示登录成功，用户名与密码一致
				}
				//String user_nickname1=resultSet.getString(3);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {

			DBUtil.release(resultSet, preparedStatement, connection);
		}

		return flag;
	}

/*
	@Override
	public UserBean logincheck(String user_name, String user_password) {
		UserBean userBean = null;
		// 验证过程，查询数据库中的用户输入名对应的密码，是否和用户自己的输入密码匹配
		String sql = "select user_password,user_id,user_nickname from users where user_name=? ";
		Connection connection = DBUtil.getConnection();
		PreparedStatement preparedStatement = null;
		ResultSet resultSet = null;

		try {
			preparedStatement = connection.prepareStatement(sql);
			// 设置对应值
			preparedStatement.setString(1, user_name);
			// 查询
			resultSet = preparedStatement.executeQuery();
			int user_id = 0;
				String password2 = resultSet.getString(1);
				if (password2.equals(MD5Tool.MD5(user_password))) {
					 user_id=resultSet.getInt(2);// 表示登录成功，用户名与密码一致
				}
				String user_nickname1=resultSet.getString(3);
				userBean=new UserBean(user_id, password2, user_nickname1);
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {

			DBUtil.release(resultSet, preparedStatement, connection);
		}

		return userBean;
	}*/

}
